package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author lizhilong
 * @create 2020/5/3 14:48
 * @desc
 */
@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @GetMapping("/payment/ok/{id}")
    public CommonResult<String> paymentOK(@PathVariable("id") Integer id ){
        String s = paymentService.payment_ok(id);
        return CommonResult.success(s);
    }

    @GetMapping("/payment/timeout/{id}")
    public CommonResult<String> paymentTimeOut(@PathVariable("id") Integer id ){
        String s = paymentService.time_out(id);
        return CommonResult.success(s);
    }

    @GetMapping("/payment/breaker/{id}")
    @HystrixCommand(fallbackMethod = "defaultMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "3000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "40")
    })
    public CommonResult<String> breakerTest(@PathVariable("id") Integer id ){
        String s = paymentService.breakerTest(id);
        return CommonResult.success(s);
    }

    public CommonResult<String> defaultMethod(@PathVariable("id") Integer id ){
        return CommonResult.failure("服务繁忙，请稍后重试");
    }

}
